﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Prodma.DataAccess.Database;
using Prodma.DataAccess.Models;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.Helper.Enumerators;
using Prodma.DataAccess.Helper.LinqExpressions;

namespace Prodma.DataAccess.Authentication
{
    public class Authentication
    {

        public static bool IsAuthenticate { get; set; }

        #region ControlAuthentication
        public bool ControlAuthentication(string username, string password)
        {
            DataContext context = new DataContext();

            password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "md5").ToLower();
            var current = (from user in context.tb_User
                           where user.UserName == username &&
                                 user.Password == password
                           //user.CARI.LK_DURUM_ID == (int)State.Active
                           select new LoginUser()
                           {
                               Id = user.ID,
                               FullName = user.FirstName + " " + user.LastName,
                               UserName = user.UserName,
                               DateTime = DateTime.Now,
                               Role = new RoleVm() { ID = user.tb_Role.ID, Ad = user.tb_Role.Ad, AddChart = user.tb_Role.AddChart, AmendOrder = user.tb_Role.AmendOrder, CompleteOrder = user.tb_Role.CompleteOrder, ConfirmOrder = user.tb_Role.ConfirmOrder, ManageUser = user.tb_Role.ManageUser, Search = user.tb_Role.Search, ClearChart = user.tb_Role.ClearChart },
                               CariKod = user.CARI.KOD,
                               IsOnline = user.IsOnline,
                               CariID = user.CariID
                               //Unvan = user.CARI.AD
                           }).FirstOrDefault();


            

            if (current == null)
            {
                return false;
            }

            // This code can see online users
            List<LoginUser> loginUserList = (List<LoginUser>)System.Web.HttpContext.Current.Application["loginUserList"];
            loginUserList.Add(current);
            System.Web.HttpContext.Current.Application["loginUserList"] = loginUserList;
            ///////////////////

            //var authorization = (from auth in context.tb_Authorization
            //                     where auth.tb_Role.Id == current.Role.ID
            //                     select new AuthorizationVm()
            //                     {
            //                         Role = new RoleVm() { ID = auth.tb_Role.Id },
            //                         MenuAdmin = new MenuAdminVm() { Id = auth.tb_MenuAdmin.Id, Name = auth.tb_MenuAdmin.Name },
            //                         Read = auth.Read,
            //                         Write = auth.Write,
            //                         Update = auth.Update,
            //                         Delete = auth.Delete,
            //                         AddChart = true,
            //                         AmendOrder = true,
            //                         ConfirmOrder = true,
            //                         CompleteOrder=true,
            //                         Search = true
            //                     }).ToList();

            RoleVm authorization = current.Role;

            if (current == null)
            {
                IsAuthenticate = false;
                HttpContext.Current.Session["loginUser"] = null;
                HttpContext.Current.Session["userAuthorization"] = null;
                return false;
            }
            else
            {                
                HttpContext.Current.Session["loginUser"] = current;
                HttpContext.Current.Session["userAuthorization"] = authorization;
                IsAuthenticate = true;
                return true;
            }
        }
        #endregion

    }
}

